package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.common.exception.CatchCustomException;
import com.usian.common.exception.code.AdminStatusCode;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.*;

@SuppressWarnings("All")
@Service
public class AdChannelServiceImpl implements AdChannelService {

    @Autowired
    private AdChannelMapper adChannelMapper;

    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        //判断参数（原则：只判断错，不判断对）
        //1、检查参数
        if (channelDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数，进行page size的初始化工作
        channelDto.checkParam();
        //分页
        Page<AdChannel> page = new Page<>(channelDto.getPage(), channelDto.getSize());
        //模糊查询(拼接条件) 构件查询对象
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //select * from adchannel where name like %name% limit 0,10    limit从0开始
    //设置查询条件 1、boolean  如果为true 执行条件，为false 不执行 2、实体类中的属性  3、查询的值
        lambdaQueryWrapper.like(channelDto.getName()!=null && !"".equals(channelDto.getName()),AdChannel::getName,channelDto.getName());//类名::方法名
        lambdaQueryWrapper.eq(channelDto.getStatus()!=null&&!"".equals(channelDto.getStatus()),AdChannel::getStatus,channelDto.getStatus());
        adChannelMapper.selectList(lambdaQueryWrapper);
        //selectPage  根据分页查询方法  1、page对象  2、查询对象
        IPage<AdChannel> iPage=adChannelMapper.selectPage(page,lambdaQueryWrapper);
//使用java 多态方式  实现向上转型
       ResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) iPage.getTotal());
responseResult.setData(iPage.getRecords());
        return responseResult;
    }

    /**
     * 添加方法
     * @param adChannel
     * @return
     */

    @Override
    public ResponseResult save(AdChannel adChannel) {
        //校验参数
        if (adChannel==null){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //添加  要开始考虑前端给我们什么数据  我们要初始化什么数据
        adChannel.setCreatedTime(new Date());//维护时间
        adChannelMapper.insert(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findById(Integer id) {
        //检查是否为空
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel=adChannelMapper.selectById(id);
        return new ResponseResult().ok(0,adChannel,"查询成功");
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //先判断对象是否为空
        if (adChannel==null){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //先查询数据库是否有对应的对象
        ResponseResult responseResult=this.findById(adChannel.getId());
        if (responseResult.getData()==null){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //修改
        adChannelMapper.updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //先判断对象是否为空
        if (id==null){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adChannelMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deletes(Integer[] ids) {
        //先判断对象是否为空
        if (ids==null || ids.length==0){
            //报错
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adChannelMapper.deleteBatchIds(Arrays.asList(ids));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public ResponseResult findAll() {
        //1、查询全部  2、根据状态  =true的  根据order字段进行排序  正序
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(AdChannel::getStatus,true);
        lambdaQueryWrapper.orderByAsc(AdChannel::getOrd);
        List<AdChannel> list=adChannelMapper.selectList(lambdaQueryWrapper);
        return ResponseResult.okResult(list);
    }
}
